package com.hsd.znsh.controller.root;

import com.alibaba.fastjson.JSONObject;
import com.hsd.znsh.entity.Property;
import com.hsd.znsh.entity.SensorType;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.service.SensorTypeService;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-07 09:12:16
 */
@RequestMapping("/root/sensor")
@RestController
public class RootSensorController {

    @Autowired
    private SensorTypeService sensorTypeService;

    @GetMapping("/getSensorTypeList")
    public ResultVO getSensorTypeListTotalPages(Integer size,SensorType sensorType){
        long totalNum=sensorTypeService.getSensorTypeListTotalPages(sensorType);
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @PostMapping("/getSensorTypeList")
    public ResultVO getSensorTypeList(Pageable pageable, SensorType sensorType){
        List<SensorType> list=sensorTypeService.getSensorTypeList(pageable,sensorType);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getSensorTypeInfo")
    public ResultVO getSensorTypeInfo(Integer id){
        SensorType sensorType=sensorTypeService.getSensorTypeInfo(id);
        return ResultVOUtil.success(sensorType);
    }

    @PostMapping("/editSensorType")
    public ResultVO editSensorType(@Valid SensorType sensorType, BindingResult bindingResult,
                                   MultipartHttpServletRequest request){
        ValidatorUtil.valid(bindingResult);
        MultipartFile sensorTypeFile=request.getFile("file");
        List<Property> propertyList=new ArrayList<>();
        List<MultipartFile> propertyFile=new ArrayList();
        for(int i=1;i<=sensorType.getValueNumber();i++){
            if(request.getParameter("propertyName"+i).equals("")){
                throw new ZnshException("属性值"+i+"名称不能为空");
            }
            if(request.getParameter("propertyAlias"+i).equals("")){
                throw new ZnshException("属性值"+i+"别名不能为空");
            }
            if(request.getParameter("propertyUnit"+i).equals("")){
                throw new ZnshException("属性值"+i+"单位不能为空");
            }
            if(request.getParameter("icon"+i)==null){
                throw new ZnshException("属性值"+i+"图标不能为空");
            }
            Property property=new Property();
            if(!StringUtils.isEmpty(request.getParameter("propertyId"+i))){
                property.setId(Integer.parseInt(request.getParameter("propertyId"+i)));
            }
            property.setName(request.getParameter("propertyName"+i));
            property.setAlias(request.getParameter("propertyAlias"+i));
            property.setUnit(request.getParameter("propertyUnit"+i));
            propertyFile.add(request.getFile("propertyFile"+i));
            propertyList.add(property);
        }
        if(sensorType.getId()==null){
            sensorTypeService.addSensorType(sensorType,sensorTypeFile,propertyList,propertyFile);
            return ResultVOUtil.success("添加成功");
        }else{
            sensorTypeService.updateSensorType(sensorType,sensorTypeFile,propertyList,propertyFile);
            return ResultVOUtil.success("修改成功");
        }
    }

    @PostMapping("/deleteSensorType")
    public ResultVO deleteSensorType(Integer id){
        sensorTypeService.deleteSensorType(id);
        return ResultVOUtil.success("删除成功");
    }


    @GetMapping("/getPropertyBySensorTypeId")
    public ResultVO getPropertyBySensorTypeId(Integer id){
        List<Property> list=sensorTypeService.getPropertyBySensorTypeId(id);
        return ResultVOUtil.success(list);
    }
}
